package
{

	import com.greensock.TweenMax;
	import com.greensock.easing.Expo;
	import com.riadeveloper.UI.Tile;
	import com.riadeveloper.UI.TileHolder;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.Security;
	
	public class Main extends Sprite
	{

		private var _boxholder:boxholder = new boxholder();
		private var tileHolder:TileHolder = new TileHolder();
		private var loadedNum:Number = 0;
		
		public function Main()
		{
			Security.allowDomain("*");
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.load(new URLRequest("data/xml/tile.xml"))
			xmlLoader.addEventListener(Event.COMPLETE,xmlLoad)
			
			
		}
		private function xmlLoad(e:Event)
		{
			loadedNum = 0; 
			var xml:XML = XML(e.target.data)
			for (var i:int = 0; i < _boxholder.numChildren; i++){
				
				var mc:Sprite = _boxholder.getChildByName("box"+i) as Sprite;				
				var tile:Tile = new Tile("tile"+i,mc);
				tile.xml =  xml.tiles.tile[i];
				
				tileHolder.addChild(tile);
				tile.id_num = i; 
				TweenMax.from(tile,.5,{x:tile.x+mc.width/2,y:tile.y+mc.height/2,scaleX:0,scaleY:0,delay:.2*i,ease:Expo.easeOut,onComplete:animateFinished})
				
			}
			addChild(tileHolder)
		}
		private function animateFinished()
		{
			loadedNum++;
			if(loadedNum == 13)
			{
				for (var i:int = 0; i < _boxholder.numChildren; i++){
					var tile:Tile = tileHolder.getChildByName("tile"+i) as Tile;
					tile.initialise();
				}
			}
		}
			
			

	}
}